Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test_take to make axis optional when ndim == 1 #192

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

asmeurer
Copy link
Member

I didn't explicitly test axis=None because it's not clear to me that should actually be supported, given that that's the same as axis=0.

Fixes #191

@asmeurer asmeurer requested a review from honno July 15, 2023 19:22
Copy link
Member

@honno honno left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM, just have a few minor points that encroach on bikesheding 😅 I think I would write this something like

def test_take(x, data):
    ...
    axis_strat = st.integers(0, max(x.ndim - 1, 0))
    if x.ndim == 1:  # i.e. axis is optional
        axis_strat |= st.none()
    axis = data.draw(axis_strat, label="axis")
    _axis = axis or 0
    _indices = data.draw(
        st.lists(st.integers(0, x.shape[_axis] - 1), min_size=1, unique=True),
        label="_indices",
    )
    indices = xp.asarray(_indices, dtype=dh.default_int)
    note(f"{indices=}")
    kw = data.draw(hh.specified_kwargs(("axis", axis, None)), label="kw")

    out = xp.take(x, indices, **kw)
    ph.assert_dtype("take", in_dtype=x.dtype, out_dtype=out.dtype)
    ph.assert_shape(
        "take",
        out_shape=out.shape,
        expected=x.shape[:_axis] + (len(_indices),) + x.shape[_axis + 1 :],
        kw=dict(
            x=x,
            indices=indices,
            axis=axis,
        ),
    )
    out_indices = sh.ndindex(out.shape)
    axis_indices = list(sh.axis_ndindex(x.shape, _axis))
    ...
  • Have a core strategy that we "add" st.none() too if relevant—if you didn't know, piping strategies is syntactic sugar for st.one_of().
  • Generate axis as the actual kwarg value, and have _axis as the processed/explicit logical value.
  • We can utilise hh.specified_kwargs() for when we want to test not passing the kwarg. test_linspace is a good example of its use.

I say all this because most of the existing tests are written to follow the above coding patterns—we don't have to be consistent or anything, just good to be aware of the prior work.

@asmeurer
Copy link
Member Author

I mentioned it in the OP, but I intentionally didn't test axis=None, because it's not clear to me that we actually want that keyword default, given that it's the same as axis=0 for the current semantics. Libraries like NumPy use axis=None to flatten the input array, and it wasn't clear that we would ever want to standardize that behavior. This is something to bring up in the spec repo.

I didn't explicitly test axis=None because it's not clear to me that should
actually be supported, given that that's the same as axis=0.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Test axis=None in test_take
2 participants